package com.xh.clean.modular.clean.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.xh.clean.core.base.controller.BaseController;
import com.xh.clean.core.common.annotion.BussinessLog;
import com.xh.clean.core.common.constant.dictmap.PatrolDict;
import com.xh.clean.core.common.constant.factory.PageFactory;
import com.xh.clean.core.log.LogObjectHolder;
import com.xh.clean.core.shiro.ShiroKit;
import com.xh.clean.core.util.DateUtil;
import com.xh.clean.modular.clean.Vo.PatrolVo;
import com.xh.clean.modular.clean.model.Patrol;
import com.xh.clean.modular.clean.model.PatrolArea;
import com.xh.clean.modular.clean.service.IPatrolAreaService;
import com.xh.clean.modular.clean.service.IPatrolService;
import com.xh.clean.modular.clean.service.IStoreyService;
import com.xh.clean.modular.clean.warpper.PatrolWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 巡视记录控制器
 *
 * @author yangjie
 * @Date 2018-06-24 14:23:48
 */
@Controller
@RequestMapping("/patrol")
public class PatrolController extends BaseController {

    private String PREFIX = "/clean/patrol/";

    @Autowired
    private IPatrolService patrolService;
    @Autowired
    private IStoreyService storeyService;
    @Autowired
    private IPatrolAreaService patrolAreaService;
    /**
     * 跳转到巡视记录首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "patrol.html";
    }

    /**
     * 跳转到添加巡视记录
     */
    @RequestMapping("/patrol_add")
    public String patrolAdd(Model model) {
        List<Map<String, Object>> list = storeyService.selectStoreys("");
        model.addAttribute("list", list);
        return PREFIX + "patrol_add.html";
    }

    /**
     * 跳转到修改巡视记录
     */
    @RequestMapping("/patrol_update/{patrolId}")
    public String patrolUpdate(@PathVariable Integer patrolId, Model model) {
        Patrol patrol = patrolService.selectById(patrolId);
        model.addAttribute("item",patrol);
        LogObjectHolder.me().set(patrol);
        List<Map<String, Object>> list = storeyService.selectStoreys("");
        model.addAttribute("list", list);
        PatrolArea patrolArea = patrolAreaService.selectById(patrol.getPareaId());
        if (patrolArea != null) {
            model.addAttribute("storeyId", patrolArea.getStoreyId());
            model.addAttribute("type", patrolArea.getType());
        }
        return PREFIX + "patrol_edit.html";
    }

    /**
     * 获取巡视记录列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(PatrolVo patrolVo) {
        Page<PatrolVo> page = new PageFactory<PatrolVo>().defaultPage();
        List<Map<String, Object>> result = patrolService.selectPatrolsByPatrolVo(page, patrolVo, page.getOrderByField(), page.isAsc());
        page.setRecords((List<PatrolVo>) new PatrolWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 新增巡视记录
     */
    @RequestMapping(value = "/add")
    @BussinessLog(value = "添加巡视记录", key = "remark,storeyId,type,pareaId", dict = PatrolDict.class)
    @ResponseBody
    public Object add(Patrol patrol) {
        patrol.setCreateDate(new Date());
        patrol.setPatrolDate(DateUtil.parseDate(DateUtil.getDay(new Date())));
        patrol.setCreater(ShiroKit.getUser().getId());
        patrolService.insert(patrol);
        return SUCCESS_TIP;
    }

    /**
     * 删除巡视记录
     */
    @RequestMapping(value = "/delete")
    @BussinessLog(value = "删除巡视记录", key = "patrolId", dict = PatrolDict.class)
    @ResponseBody
    public Object delete(@RequestParam Integer patrolId) {
        patrolService.deleteById(patrolId);
        LogObjectHolder.me().set(patrolId);
        return SUCCESS_TIP;
    }

    /**
     * 修改巡视记录
     */
    @RequestMapping(value = "/update")
    @BussinessLog(value = "修改巡视记录", key = "remark,toiletId,storeyId", dict = PatrolDict.class)
    @ResponseBody
    public Object update(Patrol patrol) {
        patrolService.updateById(patrol);
        return SUCCESS_TIP;
    }

    /**
     * 巡视记录详情
     */
    @RequestMapping(value = "/detail/{patrolId}")
    @ResponseBody
    public Object detail(@PathVariable("patrolId") Integer patrolId) {
        return patrolService.selectById(patrolId);
    }

    /**
     * 巡视记录排行榜页面
     * @return
     */
    @RequestMapping("patrol_ranking")
    public String patrolRankingPage() {
        return PREFIX + "patrol_ranking.html";
    }
   /**
   * 获取巡视记录排行榜数据
   * @Param: [patrolVo]
   * @return: java.lang.Object
   * @Author: 杨杰
   * @Date: 2018/9/2  15:01
   */ 
    @RequestMapping(value = "/patrolRanking")
    @ResponseBody
    public Object patrolRanking(PatrolVo patrolVo) {
        Page<PatrolVo> page = new PageFactory<PatrolVo>().defaultPage();
        List<Map<String, Object>> result = patrolService.selectPatrolRanking(page, patrolVo, page.getOrderByField(), page.isAsc());
        page.setRecords((List<PatrolVo>) new PatrolWarpper(result).warp());
        return super.packForBT(page);
    }
    /**
    * 巡视记录统计页面
    * @Param: [model]
    * @return: java.lang.String
    * @Author: 杨杰
    * @Date: 2018/8/26  22:19
    */ 
    @RequestMapping("/patrol_statistics")
    public String patrolStatistics(Model model) {
      /*  List<Map<String, Object>> list = storeyService.selectStoreys("");
        model.addAttribute("list", list);*/
        return PREFIX + "patrol_statistics.html";
    }
}
